package com.zdb.controller;


import com.zdb.common.Result;
import com.zdb.pojo.LongMessage;
import com.zdb.pojo.Model;
import com.zdb.pojo.Session;
import com.zdb.pojo.dto.ContentItem;
import com.zdb.pojo.dto.PromptDto;
import com.zdb.service.AiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.util.List;


@RestController
@RequestMapping("/ai")
public class AiController {
    private static final Logger log = LoggerFactory.getLogger(AiController.class);

    @Autowired
    private AiService aiService;


    @PostMapping(value = "/sse",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter SSEStreamChat(@RequestBody PromptDto promptDto){
        List<ContentItem> content = promptDto.getContent();
        String prompt=null;
        for (ContentItem contentItem : content){
            if (contentItem.getImage() != null) {
                log.info("图片:{}", contentItem.getImage());
            }
            if (contentItem.getText() != null) {
                prompt=contentItem.getText();
            }
        }
        log.info("进入sse控制器:{}", promptDto);
        return aiService.streamChatWithEmitter(prompt,promptDto.getModel(),promptDto.getId(),promptDto.getDeepThinking());
    }
    @PostMapping(value = "/send-stream",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<Result> sendStreamChat(@RequestBody PromptDto promptDto){
        List<ContentItem> content = promptDto.getContent();
        String prompt=null;
        for (ContentItem contentItem : content){
            if (contentItem.getImage() != null) {
                log.info("图片:{}", contentItem.getImage());
            }
            if (contentItem.getText() != null) {
                prompt=contentItem.getText();
            }
        }
        log.info("进入sse控制器:{}", promptDto);
        return aiService.sendStream(prompt,promptDto.getModel(),promptDto.getId(),promptDto.getDeepThinking());
    }

    @PostMapping("/create")
    public Result createSession( )
    {
        Session session = aiService.createSession();
        return Result.sucess("创建成功",session);
    }
    @GetMapping("/seesionList")
    public Result seesionList()
    {
        List<Session> sessionList = aiService.getSessionList();
        return Result.sucess("会话列表",sessionList);
    }
    @GetMapping("/modelList")
    public Result modelList()
    {
        List<Model> modelList = aiService.getModelList();
        return Result.sucess("模型列表",modelList);
    }
    @GetMapping("/message/{id}")
    public Result messageList(@PathVariable("id") int id)
    {
        List<LongMessage> messageList = aiService.getMessageList(id);
        return Result.sucess("消息列表",messageList);
    }
    @GetMapping("/sessionDetail/{id}")
    public Result sessionDetail(@PathVariable ("id") int id)
    {
        List<LongMessage> session = aiService.getSessionDetail(id);
        return Result.sucess("会话详情",session);
    }
    @GetMapping("/imageGet")
    public Result imageGet(String prompt, String model, Integer id)
    {
        return aiService.imageGet(prompt, model, id);
    }
}
